# The code environment has been reset. Re-importing necessary libraries and reloading data.
import matplotlib.pyplot as plt
from matplotlib import rcParams

# Re-setting fonts for Chinese support
rcParams['font.sans-serif'] = ['SimHei']  # Support for Chinese characters
rcParams['axes.unicode_minus'] = False  # Correct minus sign display

# Recreating data (manually adding placeholders since original data is unavailable after reset)
models = ['随机森林回归', '决策树回归']
mse_values = [26.19, 52.49]  # Example MSE values
r2_values = [0.88, 0.75]  # Example R² values
features = ['体重', 'BMI', '体力活动', '药物依从性', '压力水平', '日期天数']
feature_importances = [0.1, 0.35, 0.25, 0.15, 0.1, 0.05]  # Example feature importance
y_test = [20, 30, 40, 50, 60]  # Placeholder for actual test values
y_test_pred_dt = [22, 28, 41, 49, 62]  # Placeholder for predictions

# 1. 预测结果对比图
plt.figure(figsize=(10, 6))
plt.scatter(y_test, y_test_pred_dt, color='blue', label='决策树回归预测值')
plt.plot([min(y_test), max(y_test)], [min(y_test), max(y_test)], color='red', linestyle='--', label='理想预测线')
plt.xlabel('实际值', fontsize=14)
plt.ylabel('预测值', fontsize=14)
plt.title('决策树回归 - 实际值与预测值对比', fontsize=16)
plt.legend(fontsize=12)
plt.show()

# 2. 特征重要性图
plt.figure(figsize=(10, 6))
plt.barh(features, feature_importances, color='green')
plt.xlabel('特征重要性', fontsize=14)
plt.ylabel('特征', fontsize=14)
plt.title('决策树回归 - 特征重要性', fontsize=16)
plt.show()

# 3. 模型性能对比图
fig, ax1 = plt.subplots(figsize=(10, 6))

# MSE对比柱状图
ax1.bar(models, mse_values, color='skyblue', label='MSE')
ax1.set_xlabel('模型', fontsize=14)
ax1.set_ylabel('MSE', color='skyblue', fontsize=14)
ax1.tick_params(axis='y', labelcolor='skyblue')

# 创建第二个y轴
ax2 = ax1.twinx()
ax2.plot(models, r2_values, color='orange', marker='o', label='R²', linestyle='--')
ax2.set_ylabel('R²', color='orange', fontsize=14)
ax2.tick_params(axis='y', labelcolor='orange')

plt.title('模型性能对比：MSE与R²', fontsize=16)
plt.show()
